import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_boost/boost_navigator.dart';
import 'package:flutter_module/common/base/base_page.dart';
import 'package:flutter_module/common/extensions/cached_network_image_extension.dart';
import 'package:flutter_module/common/widget/fade_route.dart';
import 'package:flutter_module/model/cooperation_detail_mo.dart';
import 'package:flutter_module/page/cooperation/case_details_page.dart';

import '../../../routers.dart';

/// Create by kyluzoi
/// Date: 2021-08-23
///
/// Description:

class CaseShowContainer extends BaseStatelessWidget {
  final List<CaseListMo> datas;

  final bool isShowAll;

  const CaseShowContainer(
      {Key? key, required this.datas, this.isShowAll = true})
      : super(key: key);

  @override
  Widget createView(BuildContext context) {
    var items = List.generate(
        datas.length <= 4
            ? datas.length
            : isShowAll
                ? datas.length
                : 4, (index) {
      return '';
    });
    return SliverGrid(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
        childAspectRatio: 1.2,
      ),
      delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
        return index >= items.length
            ? _renderEmptyItem()
            : _renderItem(index, context);
      }, childCount: items.length % 2 == 1 ? items.length + 1 : items.length),
    );
  }

  Widget _renderItem(int index, context) {
    CaseListMo caseListMo = datas[index];
    String image = caseListMo.images?.split(",")[0] ?? "";
    return InkWell(
      onTap: () {
        BoostNavigator.instance.push(CooperationRouters.CASE_DETAILS,
            arguments: datas[index].toJson());
      },
      child: Container(
        color: Colors.white,
        child: Column(
          children: [
            Container(
                width: double.infinity,
                height: 122,
                padding: EdgeInsets.only(
                    left: index % 2 == 0 ? 16 : 4,
                    right: index % 2 != 0 ? 16 : 4),
                child: ClipRRect(
                    borderRadius: BorderRadius.circular(5),
                    child: Hero(
                      tag: "photo-$image",
                      child: YFNetworkImage(
                        resource: image,
                        fit: BoxFit.cover,
                      ),
                    ))),
            SizedBox(
              height: 3,
            ),
            Text(
              "${caseListMo.title}",
              overflow: TextOverflow.ellipsis,
              maxLines: 1,
              textAlign: TextAlign.center,
            )
          ],
        ),
      ),
    );
  }

  _renderEmptyItem() {
    return Container(
      color: Colors.white,
    );
  }
}
